package com.example.carrierapp.ui.main.dispatch.yes

import android.content.Context

import com.example.carrierapp.base.BasePresenter
import com.example.carrierapp.bean.RowsBean
import com.example.carrierapp.bean.dispatch.DispatchListBean
import com.example.carrierapp.config.toast
import com.example.carrierapp.net.ApiMiddleHelper
import com.example.carrierapp.net.listener.DataCallBackListener
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.viewmodel.itemmodel.ItemDispatchModel
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken

import java.util.HashMap

import io.reactivex.Flowable

class DispatchYesPresenter(private val mContext: Context) : BasePresenter<DispatchYesContract.View>(), DispatchYesContract.Presenter {

    override fun start() {

    }

    override fun getDispatchList(filter: String, page: Int) {
        val biddingMap = HashMap<String, String>()
        biddingMap["maxResultCount"] = "10"
        biddingMap["status"] = "2"
        biddingMap["skipCount"] = page.toString()
        biddingMap["pageIndex"] = page.toString()
        biddingMap["filter"] = filter
        val flowable = apiStrores.getDispatchList(biddingMap)

        LogUtil.d("getFindGoodsList-$filter-$page")
        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("getFindGoodsList-onSuccess-$json")

                if (view != null) {
                    view.getDispatchListSuccess(Gson().fromJson<RowsBean<ItemDispatchModel>>(json, object : TypeToken<RowsBean<ItemDispatchModel>>() {}.type))
                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("getFindGoodsList-onFailure-$msg")
                if (view != null) {

                    view.getDispatchListFailde(msg)
                }
            }
        })
    }

    override fun setTop(id: Int) {
        val biddingMap = HashMap<String, String>()
        biddingMap["id"] = id.toString()
        val flowable = apiStrores.setTop(biddingMap)
        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("sureDelivery-onSuccess-$json")
                if (view != null) {
                    view.onTopSuccess(true)

                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("sureDelivery-onFailure-$msg")
                toast(msg)
            }
        })
    }

    override fun cancelTop(id: Int) {
        val biddingMap = HashMap<String, String>()
        biddingMap["id"] = id.toString()
        val flowable = apiStrores.cancelTop(biddingMap)
        ApiMiddleHelper.getInstance().toNetIo2Main(mContext, flowable, object : DataCallBackListener {

            override fun onSuccess(json: String) {
                LogUtil.d("sureDelivery-onSuccess-$json")
                if (view != null) {
                    view.onTopSuccess(false)
                }
            }

            override fun onFailure(msg: String) {
                LogUtil.d("sureDelivery-onFailure-$msg")
                toast(msg)
            }
        })
    }
}
